home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / sendmail / sendmail-5.65c+IDA-1.4.4.1 / src / RCS / clock.c,v < prev    next >
Encoding:
Text File  |  1991-06-25  |  9.3 KB  |  578 lines

  1. head    5.8;
  2. branch    5.8.0;
  3. access;
  4. symbols
  5.     RELEASE:5.8.0.13
  6.     BETA:5.8.0.12
  7.     UICSO:5.8.0
  8.     VANILLA:5.8;
  9. locks; strict;
  10. comment    @ * @;
  11.  
  12.  
  13. 5.8
  14. date    90.06.20.08.35.23;    author paul;    state Exp;
  15. branches
  16.     5.8.0.1;
  17. next    ;
  18.  
  19. 5.8.0.1
  20. date    90.10.04.11.19.22;    author paul;    state Exp;
  21. branches;
  22. next    5.8.0.2;
  23.  
  24. 5.8.0.2
  25. date    90.10.13.17.45.48;    author paul;    state Exp;
  26. branches;
  27. next    5.8.0.3;
  28.  
  29. 5.8.0.3
  30. date    90.11.19.16.14.09;    author paul;    state Exp;
  31. branches;
  32. next    5.8.0.4;
  33.  
  34. 5.8.0.4
  35. date    90.11.24.02.38.53;    author paul;    state Exp;
  36. branches;
  37. next    5.8.0.5;
  38.  
  39. 5.8.0.5
  40. date    90.11.28.16.40.07;    author paul;    state Exp;
  41. branches;
  42. next    5.8.0.6;
  43.  
  44. 5.8.0.6
  45. date    91.01.19.19.26.02;    author paul;    state Exp;
  46. branches;
  47. next    5.8.0.7;
  48.  
  49. 5.8.0.7
  50. date    91.02.17.03.42.40;    author paul;    state Exp;
  51. branches;
  52. next    5.8.0.8;
  53.  
  54. 5.8.0.8
  55. date    91.03.04.21.48.23;    author paul;    state Exp;
  56. branches;
  57. next    5.8.0.9;
  58.  
  59. 5.8.0.9
  60. date    91.04.02.23.08.34;    author paul;    state Exp;
  61. branches;
  62. next    5.8.0.10;
  63.  
  64. 5.8.0.10
  65. date    91.04.05.14.55.15;    author paul;    state Exp;
  66. branches;
  67. next    5.8.0.11;
  68.  
  69. 5.8.0.11
  70. date    91.05.18.17.20.31;    author paul;    state Exp;
  71. branches;
  72. next    5.8.0.12;
  73.  
  74. 5.8.0.12
  75. date    91.05.30.22.11.12;    author paul;    state Exp;
  76. branches;
  77. next    5.8.0.13;
  78.  
  79. 5.8.0.13
  80. date    91.06.21.12.36.40;    author paul;    state Exp;
  81. branches;
  82. next    ;
  83.  
  84.  
  85. desc
  86. @@
  87.  
  88.  
  89. 5.8
  90. log
  91. @5.64 Berkeley release
  92. @
  93. text
  94. @/*
  95.  * Copyright (c) 1983 Eric P. Allman
  96.  * Copyright (c) 1988 Regents of the University of California.
  97.  * All rights reserved.
  98.  *
  99.  * Redistribution and use in source and binary forms are permitted provided
  100.  * that: (1) source distributions retain this entire copyright notice and
  101.  * comment, and (2) distributions including binaries display the following
  102.  * acknowledgement:  ``This product includes software developed by the
  103.  * University of California, Berkeley and its contributors'' in the
  104.  * documentation or other materials provided with the distribution and in
  105.  * all advertising materials mentioning features or use of this software.
  106.  * Neither the name of the University nor the names of its contributors may
  107.  * be used to endorse or promote products derived from this software without
  108.  * specific prior written permission.
  109.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  110.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  111.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  112.  */
  113.  
  114. #ifndef lint
  115. static char sccsid[] = "@@(#)clock.c    5.8 (Berkeley) 6/1/90";
  116. #endif /* not lint */
  117.  
  118. # include "sendmail.h"
  119. # include <signal.h>
  120.  
  121. /*
  122. **  SETEVENT -- set an event to happen at a specific time.
  123. **
  124. **    Events are stored in a sorted list for fast processing.
  125. **    An event only applies to the process that set it.
  126. **
  127. **    Parameters:
  128. **        intvl -- intvl until next event occurs.
  129. **        func -- function to call on event.
  130. **        arg -- argument to func on event.
  131. **
  132. **    Returns:
  133. **        none.
  134. **
  135. **    Side Effects:
  136. **        none.
  137. */
  138.  
  139. EVENT *
  140. setevent(intvl, func, arg)
  141.     time_t intvl;
  142.     int (*func)();
  143.     int arg;
  144. {
  145.     register EVENT **evp;
  146.     register EVENT *ev;
  147.     auto time_t now;
  148.     extern tick();
  149.  
  150.     if (intvl <= 0)
  151.     {
  152.         syserr("setevent: intvl=%ld\n", intvl);
  153.         return (NULL);
  154.     }
  155.  
  156.     (void) time(&now);
  157.  
  158.     /* search event queue for correct position */
  159.     for (evp = &EventQueue; (ev = *evp) != NULL; evp = &ev->ev_link)
  160.     {
  161.         if (ev->ev_time >= now + intvl)
  162.             break;
  163.     }
  164.  
  165.     /* insert new event */
  166.     ev = (EVENT *) xalloc(sizeof *ev);
  167.     ev->ev_time = now + intvl;
  168.     ev->ev_func = func;
  169.     ev->ev_arg = arg;
  170.     ev->ev_pid = getpid();
  171.     ev->ev_link = *evp;
  172.     *evp = ev;
  173.  
  174.     if (tTd(5, 5))
  175.         printf("setevent: intvl=%ld, for=%ld, func=%x, arg=%d, ev=%x\n",
  176.             intvl, now + intvl, func, arg, ev);
  177.  
  178.     tick();
  179.     return (ev);
  180. }
  181. /*
  182. **  CLREVENT -- remove an event from the event queue.
  183. **
  184. **    Parameters:
  185. **        ev -- pointer to event to remove.
  186. **
  187. **    Returns:
  188. **        none.
  189. **
  190. **    Side Effects:
  191. **        arranges for event ev to not happen.
  192. */
  193.  
  194. clrevent(ev)
  195.     register EVENT *ev;
  196. {
  197.     register EVENT **evp;
  198.  
  199.     if (tTd(5, 5))
  200.         printf("clrevent: ev=%x\n", ev);
  201.     if (ev == NULL)
  202.         return;
  203.  
  204.     /* find the parent event */
  205.     (void) signal(SIGALRM, SIG_IGN);
  206.     for (evp = &EventQueue; *evp != NULL; evp = &(*evp)->ev_link)
  207.     {
  208.         if (*evp == ev)
  209.             break;
  210.     }
  211.  
  212.     /* now remove it */
  213.     if (*evp != NULL)
  214.     {
  215.         *evp = ev->ev_link;
  216.         free((char *) ev);
  217.     }
  218.  
  219.     /* restore clocks and pick up anything spare */
  220.     tick();
  221. }
  222. /*
  223. **  TICK -- take a clock tick
  224. **
  225. **    Called by the alarm clock.  This routine runs events as needed.
  226. **
  227. **    Parameters:
  228. **        none.
  229. **
  230. **    Returns:
  231. **        none.
  232. **
  233. **    Side Effects:
  234. **        calls the next function in EventQueue.
  235. */
  236.  
  237. tick()
  238. {
  239.     register time_t now;
  240.     register EVENT *ev;
  241.     int mypid = getpid();
  242.  
  243.     (void) signal(SIGALRM, SIG_IGN);
  244.     (void) alarm(0);
  245.     now = curtime();
  246.  
  247.     if (tTd(5, 4))
  248.         printf("tick: now=%ld\n", now);
  249.  
  250.     while ((ev = EventQueue) != NULL &&
  251.            (ev->ev_time <= now || ev->ev_pid != mypid))
  252.     {
  253.         int (*f)();
  254.         int arg;
  255.         int pid;
  256.  
  257.         /* process the event on the top of the queue */
  258.         ev = EventQueue;
  259.         EventQueue = EventQueue->ev_link;
  260.         if (tTd(5, 6))
  261.             printf("tick: ev=%x, func=%x, arg=%d, pid=%d\n", ev,
  262.                 ev->ev_func, ev->ev_arg, ev->ev_pid);
  263.  
  264.         /* we must be careful in here because ev_func may not return */
  265.         (void) signal(SIGALRM, tick);
  266. #ifdef SIGVTALRM
  267.         /* reset 4.2bsd signal mask to allow future alarms */
  268.         (void) sigsetmask(sigblock(0) & ~sigmask(SIGALRM));
  269. #endif SIGVTALRM
  270.  
  271.         f = ev->ev_func;
  272.         arg = ev->ev_arg;
  273.         pid = ev->ev_pid;
  274.         free((char *) ev);
  275.         if (pid != getpid())
  276.             continue;
  277.         if (EventQueue != NULL)
  278.         {
  279.             if (EventQueue->ev_time > now)
  280.                 (void) alarm((unsigned) (EventQueue->ev_time - now));
  281.             else
  282.                 (void) alarm(3);
  283.         }
  284.         (*f)(arg);
  285.         (void) alarm(0);
  286.         now = curtime();
  287.     }
  288.     (void) signal(SIGALRM, tick);
  289.     if (EventQueue != NULL)
  290.         (void) alarm((unsigned) (EventQueue->ev_time - now));
  291. }
  292. /*
  293. **  SLEEP -- a version of sleep that works with this stuff
  294. **
  295. **    Because sleep uses the alarm facility, I must reimplement
  296. **    it here.
  297. **
  298. **    Parameters:
  299. **        intvl -- time to sleep.
  300. **
  301. **    Returns:
  302. **        none.
  303. **
  304. **    Side Effects:
  305. **        waits for intvl time.  However, other events can
  306. **        be run during that interval.
  307. */
  308.  
  309. static bool    SleepDone;
  310.  
  311. sleep(intvl)
  312.     unsigned int intvl;
  313. {
  314.     extern endsleep();
  315.  
  316.     if (intvl == 0)
  317.         return;
  318.     SleepDone = FALSE;
  319.     (void) setevent((time_t) intvl, endsleep, 0);
  320.     while (!SleepDone)
  321.         pause();
  322. }
  323.  
  324. static
  325. endsleep()
  326. {
  327.     SleepDone = TRUE;
  328. }
  329. @
  330.  
  331.  
  332. 5.8.0.1
  333. log
  334. @Added static declaration for endsleep() to make gcc happy.
  335. @
  336. text
  337. @d221 1
  338. a221 1
  339.     static endsleep();
  340. @
  341.  
  342.  
  343. 5.8.0.2
  344. log
  345. @Shifted position of #include "sendmail.h" to avoid SIGCHLD re-definition
  346. warning on System 5 platforms.
  347. @
  348. text
  349. @d25 1
  350. a26 1
  351. # include "sendmail.h"
  352. a217 3
  353. #ifdef hpux
  354. unsigned int
  355. #endif /* hpux */
  356. @
  357.  
  358.  
  359. 5.8.0.3
  360. log
  361. @Replaced #ifdef hpux with the more generic #ifdef XPG3 (X-Open portability
  362. guide #3).  From Andy Linton (andy.linton@@comp.vuw.ac.nz).
  363. @
  364. text
  365. @d218 1
  366. a218 1
  367. #ifdef XPG3
  368. d220 1
  369. a220 1
  370. #endif /* XPG3 */
  371. @
  372.  
  373.  
  374. 5.8.0.4
  375. log
  376. @Fixed forward declaration of endsleep().
  377. @
  378. text
  379. @d25 2
  380. a26 2
  381. #include <signal.h>
  382. #include "sendmail.h"
  383. d176 1
  384. a176 1
  385. #endif /* SIGVTALRM */
  386. a216 1
  387. void        endsleep();
  388. d224 2
  389. d234 1
  390. a234 1
  391. static void
  392. @
  393.  
  394.  
  395. 5.8.0.5
  396. log
  397. @Declare tick() to be SIG_TYPE.
  398. @
  399. text
  400. @a27 2
  401. SIG_TYPE tick();
  402.  
  403. d55 1
  404. a143 1
  405. SIG_TYPE
  406. @
  407.  
  408.  
  409. 5.8.0.6
  410. log
  411. @Deleted #include <sys/types.h> as it's already included via sendmail.h from
  412. useful.h.  #include "sendmail.h" relocated to top of #include list.
  413. @
  414. text
  415. @d25 1
  416. a26 1
  417. #include <signal.h>
  418. @
  419.  
  420.  
  421. 5.8.0.7
  422. log
  423. @Added static keyword to tick() declaration.
  424. @
  425. text
  426. @d28 1
  427. a28 1
  428. static SIG_TYPE tick();
  429. d145 1
  430. a145 1
  431. static SIG_TYPE
  432. @
  433.  
  434.  
  435. 5.8.0.8
  436. log
  437. @ANSIfied.
  438. @
  439. text
  440. @d28 1
  441. a28 2
  442. static void tick();
  443. static void endsleep();
  444. d51 1
  445. a51 1
  446.     void (*func)();
  447. a101 1
  448. void
  449. d145 1
  450. a145 1
  451. static void
  452. d162 1
  453. a162 1
  454.         void (*f)();
  455. d202 1
  456. a202 1
  457. **  XSLEEP -- a version of sleep that works with this stuff
  458. d219 1
  459. d221 4
  460. a224 2
  461. void
  462. Xsleep(intvl)
  463. @
  464.  
  465.  
  466. 5.8.0.9
  467. log
  468. @Include <sys/signal.h>, not <signal.h>.
  469. @
  470. text
  471. @d26 1
  472. a26 1
  473. #include <sys/signal.h>
  474. d28 1
  475. a28 1
  476. static SIG_TYPE tick();
  477. d147 1
  478. a147 1
  479. static SIG_TYPE
  480. @
  481.  
  482.  
  483. 5.8.0.10
  484. log
  485. @Added RCS ID string
  486. @
  487. text
  488. @a22 1
  489. static char rcsid[] = "@@(#)$Id$";
  490. @
  491.  
  492.  
  493. 5.8.0.11
  494. log
  495. @Now uses TIME_TYPE for time() arguments.
  496. @
  497. text
  498. @d23 1
  499. a23 1
  500. static char rcsid[] = "@@(#)$Id: clock.c,v 5.8.0.10 1991/04/05 14:55:15 paul Exp paul $";
  501. d58 1
  502. a58 1
  503.     auto TIME_TYPE now;
  504. d71 1
  505. a71 1
  506.         if (ev->ev_time >= (time_t)now + intvl)
  507. d77 1
  508. a77 1
  509.     ev->ev_time = (time_t)now + intvl;
  510. d86 1
  511. a86 1
  512.             intvl, (time_t)now + intvl, func, arg, ev);
  513. @
  514.  
  515.  
  516. 5.8.0.12
  517. log
  518. @Use sigprocmask if LACK_SIGBLOCK is defined.
  519. @
  520. text
  521. @d23 1
  522. a23 1
  523. static char rcsid[] = "@@(#)$Id: clock.c,v 5.8.0.11 1991/05/18 17:20:31 paul Exp paul $";
  524. a179 9
  525. # ifdef LACK_SIGBLOCK
  526.         {
  527.             sigset_t set = SIGALRM;
  528.             (void) sigprocmask(SIG_UNBLOCK, &set, NULL);
  529.         }
  530. # else /* !LACK_SIGBLOCK */
  531. #  ifndef sigmask
  532. #   define sigmask(m)    (1<<((m)-1))
  533. #  endif /* !sigmask */
  534. a180 1
  535. # endif /* LACK_SIGBLOCK */
  536. @
  537.  
  538.  
  539. 5.8.0.13
  540. log
  541. @Use TIME_TYPE instead of time_t.
  542. @
  543. text
  544. @d23 1
  545. a23 1
  546. static char rcsid[] = "@@(#)$Id: clock.c,v 5.8.0.12 1991/05/30 22:11:12 paul Exp paul $";
  547. d52 1
  548. a52 1
  549.     TIME_TYPE intvl;
  550. d71 1
  551. a71 1
  552.         if (ev->ev_time >= (TIME_TYPE)now + intvl)
  553. d77 1
  554. a77 1
  555.     ev->ev_time = (TIME_TYPE)now + intvl;
  556. d86 1
  557. a86 1
  558.             intvl, (TIME_TYPE)now + intvl, func, arg, ev);
  559. d88 1
  560. a88 1
  561.     (void) tick();
  562. d131 1
  563. a131 1
  564.     (void) tick();
  565. d151 1
  566. a151 1
  567.     register TIME_TYPE now;
  568. a212 5
  569. #ifdef notdef
  570. #if ( SIG_TYPE == int )
  571.     return(0);
  572. #endif /* SIG_TYPE == int */
  573. #endif /* notdef */
  574. d240 1
  575. a240 1
  576.     (void) setevent((TIME_TYPE) intvl, endsleep, 0);
  577. @
  578.